package Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hjh.qinxin.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import adpter.HotListAdapter;
import adpter.PopularizeListAdapter;
import adpter.ReportListAdapter;
import model.HotListModel;
import model.PopularizeListModel;
import model.ReportListModel;
import retrofit2.Call;
import retrofit2.Callback;
import web.BaseApplication;
import web.BrandPromotion.BrandResponse;
import web.BrandPromotion.ValuesBrand;
import web.HotReport.HorReportRespon;
import web.HotReport.ValuesHotReport;
import web.HotSearch.Response;
import web.HotSearch.ValuesSearch;


public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root =  inflater.inflate(R.layout.fragment_home, container, false);
        /*root.findViewById(R.id.homeft_popularize_more)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getFragmentManager()
                                .beginTransaction()
                                .addToBackStack(null)
                                .replace(R.id.home_container, new BrandFragment())
                                .commit();
                    }
                });*/
        HotList(root);
        PopularizeList(root);
        ReportList(root);
        return root;
    }

    private void HotList(View root){
        TextView textView = root.findViewById(R.id.hot_more);
        List<HotListModel> data = new ArrayList<>();
        RecyclerView recyclerView = root.findViewById(R.id.hot_list);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(llm);

        HotListAdapter adapter = new HotListAdapter(getActivity());
        adapter.setData(data);
        recyclerView.setAdapter(adapter);

        Call<Response<ValuesSearch>> call = BaseApplication.httpbinService.recycleText("android");
        call.enqueue(new Callback<Response<ValuesSearch>>() {
            @Override
            public void onResponse(Call<Response<ValuesSearch>> call, retrofit2.Response<Response<ValuesSearch>> response) {
                for (int i = 0; i < response.body().getValues().getData().size(); i++) {
                    HotListModel item = new HotListModel();
                    item.setHotlistText(response.body().getValues().getData().get(i).getTitle());
                    item.setUrl(response.body().getValues().getData().get(i).getUrl());
                    data.add(item);
                }
                adapter.notifyDataSetChanged();

                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        data.clear();
                        HotList(root);
                    }
                });

            }


            @Override
            public void onFailure(Call<Response<ValuesSearch>> call, Throwable t) {

            }
        });
    }
    private void PopularizeList(View root){
        List<PopularizeListModel> data = new ArrayList<>();
        RecyclerView recyclerView = root.findViewById(R.id.homeft_popularize_list);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(llm);

        PopularizeListAdapter adapter = new PopularizeListAdapter(getActivity());
        adapter.setData(data);
        recyclerView.setAdapter(adapter);

        Call<BrandResponse<ValuesBrand>> call = BaseApplication.httpbinService.recyclePicture();
        call.enqueue(new Callback<BrandResponse<ValuesBrand>>() {
            @Override
            public void onResponse(Call<BrandResponse<ValuesBrand>> call, retrofit2.Response<BrandResponse<ValuesBrand>> response) {
                for (int i = 0; i < response.body().getValues().getTui_brand().size(); i++) {
                    PopularizeListModel item = new PopularizeListModel();
                    item.setUrl(response.body().getValues().getTui_brand().get(i).getUrl());
                    item.setListPicture(response.body().getValues().getTui_brand().get(i).getCompany_name());
                    data.add(item);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<BrandResponse<ValuesBrand>> call, Throwable t) {

            }
        });
    }
    private void ReportList(View root){
        List<ReportListModel> data  =new ArrayList<>();
        RecyclerView recyclerView = root.findViewById(R.id.homeft_report_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        ReportListAdapter adapter = new ReportListAdapter(getActivity());
        adapter.setData(data);
        recyclerView.setAdapter(adapter);
        Call<HorReportRespon<ValuesHotReport>> call = BaseApplication.httpbinService.HotReport();
        call.enqueue(new Callback<HorReportRespon<ValuesHotReport>>() {
            @Override
            public void onResponse(Call<HorReportRespon<ValuesHotReport>> call, retrofit2.Response<HorReportRespon<ValuesHotReport>> response) {
                for (int i = 0;i < response.body().getValues().getTui_baodao_news().size();i++){
                    ReportListModel item = new ReportListModel();
                    item.setUrl(response.body().getValues().getTui_baodao_news().get(i).getUrl());
                    item.setListHeadline(response.body().getValues().getTui_baodao_news().get(i).getTitle());
                    item.setListTime(response.body().getValues().getTui_baodao_news().get(i).getAdd_time());
                    data.add(item);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<HorReportRespon<ValuesHotReport>> call, Throwable t) {

            }
        });

    }

}